package net.wofly.sale.exchange.web;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.condition.Condition;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.ReturnBean;
import net.wofly.common.web.BaseController;
import net.wofly.sale.exchange.domain.CardExchange;
import net.wofly.sale.exchange.domain.ExchangeConsume;
import net.wofly.sale.exchange.domain.ExchangeStock;
import net.wofly.sale.exchange.service.IExchangeService;
import net.wofly.sale.exchange.validator.ExchangeValidator;

/**
 * @author lign
 * @version 1.0
 * @created 28-九月-2017 21:36:38
 */
@RestController
@RequestMapping(value = "/api/exchange")
@Api(description = "兑换接口")
public class ExchangeController extends BaseController {

	@Autowired
	IExchangeService iExchangeService;

	/**
	 * 添加兑换
	 * <ul>
	 * 	<li>添加后，自动增加相应的库存</li>
	 * 	<li></li>
	 * </ul>
	 * 
	 * @param cardExchange
	 */
	@ApiOperation(value = "添加兑换", notes = "添加兑换")
	@ApiImplicitParam(name = "cardExchange", value = "卡兑换实体", required = true, dataType = "CardExchange")
	@RequestMapping(value = "/addCardExchange", method = {RequestMethod.POST})
	public ReturnBean<CardExchange> addCardExchange(@RequestBody CardExchange cardExchange){
		ReturnBean<CardExchange> returnBean;
		String errorMessage = ExchangeValidator.validateAddCardExchangeParams(cardExchange);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iExchangeService.addCardExchange(cardExchange);
		}
		return returnBean;
	}

	/**
	 * 增加兑换消费
	 * <ul>
	 * 	<li>增加后自动减动当前用户兑换库存</li>
	 * 	<li></li>
	 * </ul>
	 * 
	 * @param exchangeConsume
	 */
	@ApiOperation(value = "增加兑换消费", notes = "增加兑换消费")
	@ApiImplicitParam(name = "cardExchange", value = "卡兑换实体", required = true, dataType = "CardExchange")
	@RequestMapping(value = "/addExchangeConsume", method = {RequestMethod.POST})
	public ReturnBean<ExchangeConsume> addExchangeConsume(ExchangeConsume exchangeConsume){
		ReturnBean<ExchangeConsume> returnBean;
		String errorMessage = ExchangeValidator.validateAddExchangeConsumeParams(exchangeConsume);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iExchangeService.addExchangeConsume(exchangeConsume);
		}
		return returnBean;
	}

	/**
	 * 分页获取兑换库存
	 * <ul>
	 * 	<li></li>
	 * </ul>
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param condition
	 */
	@ApiOperation(value = "分页获取兑换库存", notes = "分页获取兑换库存")
	@ApiImplicitParams({@ApiImplicitParam(name = "condition", value = "查询条件", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "第几页", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页多少条", required = false, dataType = "Integer", paramType = "query")})
	@RequestMapping(value = "/getAllExchangeStock", method = {RequestMethod.GET})
	public ReturnBean<PageWrapper<ExchangeStock>> getAllExchangeStock(@RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
																	  @RequestParam(defaultValue = "20", name = "pageSize") Integer pageSize,
																	  String condition){
		ReturnBean<PageWrapper<ExchangeStock>> returnBean;
		String errorMessage = ExchangeValidator.validateGetAllExchangeStockParams(condition);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			Condition condition1 = null;
			if (condition != null) {
				condition1 = JSON.parseObject(condition, Condition.class);
			}
			returnBean = iExchangeService.getAllExchangeStock(pageNum, pageSize, condition1);
		}
		return returnBean;
	}

	/**
	 * 
	 * @param userID
	 */
	@ApiOperation(value = "获取我的兑换记录", notes = "获取我的兑换记录")
	@ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "第几页", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页多少条", required = false, dataType = "Integer", paramType = "query")})
	@RequestMapping(value = "/getMyCardExchange", method = {RequestMethod.GET})
	public ReturnBean<PageWrapper<CardExchange>> getMyCardExchange(String userID, @RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
																   @RequestParam(defaultValue = "20", name = "pageSize") Integer pageSize){
		ReturnBean<PageWrapper<CardExchange>> returnBean;
		String errorMessage = ExchangeValidator.validateGetMyCardExchangeParams(userID);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iExchangeService.getMyCardExchange(userID, pageNum, pageSize);
		}
		return returnBean;
	}
	
	/**
	 * 
	 * @param userID
	 */
	@ApiOperation(value = "获取全部兑换记录", notes = "获取全部兑换记录")
	@ApiImplicitParams({@ApiImplicitParam(name = "salesUserID", value = "销售UserID", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "customerUserID", value = "客户UserID", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "第几页", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页多少条", required = true, dataType = "Integer", paramType = "query")})
	@RequestMapping(value = "/getAllCardExchange", method = {RequestMethod.GET})
	public ReturnBean<PageWrapper<CardExchange>> getAllCardExchange(String salesUserID, String customerUserID , @RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
																   @RequestParam(defaultValue = "20", name = "pageSize") Integer pageSize){
		
		return iExchangeService.getAllCardExchange(salesUserID,customerUserID ,pageNum, pageSize) ;
	}

	/**
	 * 
	 * @param userID
	 */
	@ApiOperation(value = "获取兑换消费记录", notes = "获取兑换消费记录")
	@ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "第几页", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页多少条", required = false, dataType = "Integer", paramType = "query")})
	@RequestMapping(value = "/getMyExchangeConsume", method = {RequestMethod.GET})
	public ReturnBean<PageWrapper<ExchangeConsume>> getMyExchangeConsume(String userID, @RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
																		 @RequestParam(defaultValue = "20", name = "pageSize") Integer pageSize){
		ReturnBean<PageWrapper<ExchangeConsume>> returnBean;
		String errorMessage = ExchangeValidator.validateGetMyExchangeConsumeParams(userID);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iExchangeService.getMyExchangeConsume(userID, pageNum, pageSize);
		}
		return returnBean;
	}

	/**
	 * 获取指定用户的兑换库存
	 * 
	 * @param userID
	 */
	@ApiOperation(value = "获取指定用户的兑换库存", notes = "获取指定用户的兑换库存")
	@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String", paramType = "query")
	@RequestMapping(value = "/getMyExchangeStock", method = {RequestMethod.GET})
	public ReturnBean<ExchangeStock> getMyExchangeStock(String userID){
		ReturnBean<ExchangeStock> returnBean;
		String errorMessage = ExchangeValidator.validateGetMyExchangeStockParams(userID);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iExchangeService.getMyExchangeStock(userID);
		}
		return returnBean;

	}
	
	/**
	 * 获取指定用户的兑换库存
	 * 
	 * @param userID
	 */
	@ApiOperation(value = "获取指定用户的兑换库存", notes = "获取指定用户的兑换库存")
	@ApiImplicitParam(name = "cardExchangeID", value = "用户ID", required = true, dataType = "String", paramType = "query")
	@RequestMapping(value = "/getCardExchangeDetail", method = {RequestMethod.GET})
	public ReturnBean<CardExchange> getCardExchangeDetail(String cardExchangeID){
		ReturnBean<CardExchange> returnBean;
		String errorMessage = ExchangeValidator.validateGetMyExchangeStockParams(cardExchangeID);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iExchangeService.getCardExchangeDetail(cardExchangeID);
		}
		return returnBean;

	}

}